/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.ejb.metier;

import java.security.MessageDigest;

/**
 *
 * @author Wanted
 * Class used to crypt password with SHA1
 * 
 */
public class Encryption {
    
    /**
     * 
     * Crypt the password with SHA1
     * @param password that must be crypted
     * 
     */
    public static String cryptPassword(String password){
        try {
            MessageDigest md = MessageDigest.getInstance("SHA1");
            md.update(password.getBytes()); 
            byte[] output = md.digest();
            password = hexString(output);
        } catch (Exception e) {
        } 
        return password;
        
    }
    
    /**
     * 
     * Return the password crypted in String
     * @param buf Bytes version of the password crypted
     * @return String
     * 
     */
    private static String hexString( byte[] buf ) {
        char[] TAB_BYTE_HEX = { '0', '1', '2', '3', '4', '5', '6','7',
                                '8', '9', 'a', 'b', 'c', 'd', 'e','f' };
 
        StringBuffer sb = new StringBuffer( buf.length*2 );
 
        for ( int i=0; i<buf.length; i++ ) {
            sb.append( TAB_BYTE_HEX[(buf[i]>>>4) & 0xf] );
            sb.append( TAB_BYTE_HEX[ buf[i]      & 0x0f] );
        }
        return sb.toString();
   }
    
}
